<?php

namespace App\Models;

use App\Common\Constant\Params;
use Illuminate\Database\Eloquent\Model;


class OrderDetail extends BaseModel
{

    protected $table = 'orders_detail';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'order_id',
        'customer_name',
        'customer_phone',
        'customer_area_id',
        'customer_address',
        'customer_elevator',
        'customer_floor',
        'customer_tmall_number',
        'customer_memark',
        'logistics_packages',
        'logistics_ticketnumber',
        'logistics_name',
        'logistics_phone',
        'logistics_address',
        'logistics_consignee',
        'logistics_mark',
        'merchant_name',
        'merchant_phone',
        'merchant_finish_time',
        'customer_area',
    ];

    protected $casts = [
        'merchant_finish_time' => 'datetime',
    ];

    protected $touches = ['order'];

    public function order(){
        return $this->belongsTo(Order::class,'order_id');
    }

    /**
     * 获取电梯/步梯文本
     */
    public function getCustomerElevatorTextAttribute()
    {
        $this->customer_elevator = $this->customer_elevator == 0 ? 1:$this->customer_elevator;
        return Params::$ELEVATOR[$this->customer_elevator];
    }

    /**
     * 获取客户所在地址文本
     * customer_area_text
     */
    public function getCustomerAreaTextAttribute()
    {
        if (!$this->customer_area_id){
            return;
        }
        $areas = getAreas();
        $district = $areas[$this->customer_area_id];
        $city = $areas[$district['p']];
        if(!array_key_exists('p',$city)) {
            return '未知区域';
        }
        $province = $areas[$city['p']];
        return implode('-',[$province['n'],$city['n'],$district['n']]);
    }

    /**
     * 商家希望完成时间文本
     */
    public function getMerchantFinishTimeTextAttribute()
    {
        return sprintf('%s(%s)',
            $this->merchant_finish_time->format('m月d日'),
            Params::$WEEKS[$this->merchant_finish_time->format('N')]);
    }

}
